﻿using System.Collections.Generic;
using System.Linq;
using com.cruxomatic.DTOs;
using com.cruxomatic.BusinessObjects;
using com.cruxomatic.BusinessObjects.DatabaseQueries;
using com.cruxomatic.exceptions;

namespace com.cruxomatic.BusinessFacades
{
    public class DepartmentFacade
    {
        public List<DepartmentInformation> GetDepartments(int CompanyID)
        {
            Queries departmentsQuery = new Queries();
            IEnumerable<DepartmentInformation> departments =
                from departmentsList in departmentsQuery.GetDepartments(CompanyID)
                select ClassConverter.CopyDepartmentToDepartmentInformation(departmentsList);
            return departments.ToList();
        }

        public List<DepartmentInformation> GetDepartmentsForUser(int UserID)
        {
            Queries departmentsQuery = new Queries();
            IEnumerable<DepartmentInformation> departments =
                from departmentsList in departmentsQuery.GetDepartmentsForUser(UserID)
                select ClassConverter.CopyDepartmentToDepartmentInformation(departmentsList);
            return departments.ToList();
        }

        public DepartmentInformation GetDepartment(int DepartmentID)
        {
            if (DepartmentID == 0) return new DepartmentInformation();
            Queries departmentQuery = new Queries();
            Department department = departmentQuery.GetDepartment(DepartmentID);
            return ClassConverter.CopyDepartmentToDepartmentInformation(department);
        }

        public void SaveDepartment(DepartmentInformation department)
        {
            CheckValidity(department);
            Queries departmentQuery = new Queries();
            Department dbDepartment = new Department();
            if (department.DepartmentID != 0)
                dbDepartment = departmentQuery.GetDepartment(department.DepartmentID);
            dbDepartment = ClassConverter.CopyDeparmentInformationToDepartment(department, dbDepartment);
            if (department.DepartmentID != 0)
                departmentQuery.SubmitChanges();
            else
                departmentQuery.InsertDepartment(dbDepartment);

        }

        private void CheckValidity(DepartmentInformation department)
        {
            if (DoesDepartmentNameExistInCompany(department, department.CompanyName))
                throw CruxExceptionManager.CreateNewDatabaseException("902");
        }

        public bool DoesDepartmentNameExistInCompany(DepartmentInformation department, string CompanyName)
        {
            Queries query = new Queries();
            Department dbDepartment = query.GetDepartment(CompanyName, department.DepartmentName);
            if (dbDepartment != null && dbDepartment.DepartmentID != department.DepartmentID) 
                return true;
            else 
                return false;
        }

        public void DeleteDepartment(int DepartmentID)
        {
            Queries departmentQuery = new Queries();
            Department department = departmentQuery.GetDepartment(DepartmentID);
            departmentQuery.DeleteDepartment(department);
        }
    }
}
